
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
	<meta charset="UTF-8">
    <title>出库登记复核页面</title>
    <link rel="stylesheet"  href="/bootstrap/bootstrap-5.3.3-dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="/css/stock/h/Warehouse_Receving_Registration_sub.css" />
    <style type="text/css">
    
    </style>
</head>
<body style="padding: 15px 50px;background-color: #eeeeee;">
	<div class="panel-heading hhead">
		<nav aria-label="breadcrumb">
		  <ol class="breadcrumb">
		  	<span style="color: blue;">您正在做的业务是：</span>
		    <li class="breadcrumb-item"><a href="#">库存管理</a></li>
		    <li class="breadcrumb-item"><a href="#">出库管理</a></li>
		    <li class="breadcrumb-item active" aria-current="page">出库登记复核</li>
		  </ol>
		</nav>
	</div>
    <div style="margin-bottom: 2px">
        <div style="display: inline;margin-left: 85%;">
            <button type="button" class="btn btn-primary" id="submit">复核通过</button>
            <button type="button" class="btn btn-primary" id="back">返回</button>
        </div>
    </div> <!-- 此div改用栅格来做 -->
    <div id="designListBox">
        <div style="text-align: center">
            <h3>出库单</h3>
        </div>
        <div style="padding: 30px">
           <table class="tbl1">
               <tr>
                   <td class="col1">出库单编号：</td>
                   <td class="col2"  id="payId">aaa</td>
               </tr>
               <tr>
                   <td class="col1">出库理由：</td>
                   <td class="col2" id="reason"></td>
                   <td class="col1">出库详情理由：</td>
                   <td class="col2"  id="reasonexact"></td>
               </tr>
               
           </table>

            <table class="table table-bordered" style="margin: 15px auto;">
                <thead>
                    <tr>
                        <th>产品编号</th><th>产品名称</th><th>库存名称</th>
                        <th>存放地址</th><th>应出库件数</th><th>已出库件数</th>
                        <th>本次出库数量</th><th>复核序列号</th>
                    </tr>
                </thead>
                <tbody id="template">
<%--                    <tr>--%>
<%--                        <td>1</td>--%>
<%--                        <td>aaaaaaaaaaaaaaaaaaaa</td>--%>
<%--                        <td>aaaaaaaaaaaaa</td>--%>
<%--                        <td>a</td>--%>
<%--                        <td>a</td>--%>
<%--                        <td>a</td>--%>
<%--                        <td class="num"><input class="num-input"></td>--%>
<%--                        <td>a</td>--%>
<%--                    </tr>--%>
                </tbody>
            </table>

            <table class="tbl3">
                <tr>
                    <td class="col1">应出库总件数：</td>
                    <td class="col2" id="Should"></td>
                    <td class="col1">已出库总件数：</td> <td class="col2"  id="already"></td>
                </tr>
                <tr>
                    <td class="col1">应出库总成本：</td>
                    <td class="col2"  id="ShouldPrice"></td>
                    <td class="col1">已出库总成本：</td> <td class="col2"  id="alreadyPrice"></td>
                </tr>
                <tr>
                    <td class="col1">复核人：</td>
                    <td class="col2" id="checker"></td>
                    <td class="col1">复核时间：</td> <td class="col2" id="checkerTime">系统时间</td>
                </tr>
            </table>
        </div>
    </div>
    <script type="text/javascript" src="/js/jquery-3.7.0.min.js"></script>
    <script type="text/javascript" src="/bootstrap/bootstrap-5.3.3-dist/js/bootstrap.bundle.min.js"></script>
    <script type="text/javascript" src="/js/common.js"></script>
    <script type="text/javascript">
        $(function (){
            // 初始化数据
            ItemApprovalTable();
            $("#back").on("click",function (){
                window.location.href="/s-pay/Warehouse-out_Registration_review";
            })
            $("#submit").on("click",function (){
                //获取出库单编号
                let payId = $("#payId").text();
                //获取应出库总件数
                let should = $("#Should").text();
                let reasonexact = $("#reasonexact").text().trim();
                let data = [];
                data.push({
                    //出库单编号
                    payId: payId,
                    //确认出库总件数
                    paidAmountSum: should,
                    reasonexact: reasonexact

                })
                let rows = $("#template tr");
                for (let i = 0; i < rows.length; i++) {
                    let row = rows[i];
                    let productId = $(row).find("td:eq(0)").text();
                    let paidAmount= $(row).find("td:eq(6)").find("input").val();
                    let obj = {
                        productId: productId,
                        paidAmount: paidAmount
                    }
                    data.push(obj);
                }
                data=JSON.stringify(data);
                console.log("复核通过所需数据："+data);

                $.ajax({
                    url: "/s-pay/OutboundRecheck",
                    type: "POST",
                    data:  data,
                    contentType: "application/json",
                    dataType: "json",
                    success: function (flag) {
                        if (flag){
                            alert("本次复核成功，本出库单全部完成,请返回！");
                            window.location.href = "/s-pay/Warehouse-out_Registration_review";
                        }else{
                            alert("本次复核失败！");
                        }


                    },
                    error: function (e) {
                    }
                })
            })
        })
        function ItemApprovalTable(){
            let id = sessionStorage.getItem("id");
            $("#id").val(id);
            $.ajax({
                url: "/s-pay/ItemApprovalTable?id="+id,
                type: "GET",
                dataType: "json",
                success: function (data) {
                    // console.log(data);
                    if (data.errCode == "1") {
                        alert(data.errMsg)
                    } else {
                        $("#payId").text(data.payId);
                        $("#reason").text(data.reason);
                        $("#reasonexact").text(data.reasonexact);
                        $("#spayId").attr("value",data.id)
                        $("#ShouldPrice").text(data.costPriceSum);
                        $("#checker").text(data.checker)
                        $("#checkerTime").text(data.checkTime);
                        initialTemplate(data.id);

                    }
                }
            })
        }
        function initialTemplate(parentId){
            $.ajax({
                url: "/s-pay/initialTemplate?id="+parentId,
                type: "GET",
                dataType: "json",
                success: function (data) {
                    // console.log(data);
                    if (data.errCode == "1") {
                        alert(data.errMsg)
                    } else {
                        let template = $("#template");

                        // 清空表格
                        template.empty();
                        // 遍历数据并添加到表格中
                        data.forEach(function (item, index) {

                            let w=  Add(item.productId);

                            let amount =AddAmount(item.productId);
                            let row = $("<tr>");
                            row.append($("<td>").text(item.productId));
                            row.append($("<td>").text(item.productName));
                            row.append($("<td>").text(w))
                            row.append($("<td>").text(amount).addClass("current"))
                            row.append($("<td>").text(item.paidAmount).addClass("should"));
                            row.append($("<td>").text("0").append($("<input>").attr("type","hidden").attr("value",item.costPrice)));
                            //input只读
                            row.append($("<td>").append($("<input>").addClass("num-input").attr("type","number").attr("readonly",true).attr("value",item.paidAmount)));
                            // row.append($("<td>").append($("<input>").addClass("num-input").attr("type","number").attr("value",item.paidAmount)));
                            template.append(row);

                        })
                        //初始化应出库总件数和已出库总件数
                        initialTotalSum();
                        initalalreadyPrice();
                    }
                }
            })
        }

        function initialTotalSum(){
            // 初始化总和变量
            let totalOutbound = 0;
            let totalAlready = 0;
            // 选择表格中tbody下所有的tr元素，然后找到每个tr里面的第四列（索引为3，因为索引从0开始计数）
            $('#template tr').each(function () {
                let num = parseInt($(this).find('td').eq(4).text());
                let num2 = parseInt($(this).find('td').eq(5).text());
                totalOutbound += num;
                totalAlready += num2;
            });

            $("#Should").text(totalOutbound);
            $("#already").text(totalAlready);
        }
        function Add(productId){
            let warehouseName ="";
            $.ajax({
                url: "/s-warehouse-setting/getWarehouseName",
                type: "POST",
                async: false,
                data: productId,
                contentType: "application/json",
                dataType: "json",
                success: function (data) {
                    // console.log("库房名称：" + data.warehouseName);
                    warehouseName = data.warehouseName;
                }
                ,error:function (e){

                }
            })


            return warehouseName;
        }
        function AddAmount(productId){
            let amount = 0;
            //得到当前存储量
            $.ajax({
                url: "/s-pay/initialAmount",
                type: "POST",
                async: false,
                data: productId,
                contentType: "application/json",
                dataType: "json",
                success: function (data) {
                    amount= data.sCell.amount;
                },
                error: function (e) {
                    console.log(e);
                }
            })
            return amount;
        }
        function initalalreadyPrice(){
            //设置已出库总成本
            let alreadyPrice = 0;
            $("#template tr").each(function (){
                // 获取已出库数量
                let already = $(this).find("td").eq(5).text();
                let price = $(this).find("td").eq(5).find("input").val();
                alreadyPrice +=already* price;
                $("#alreadyPrice").text(alreadyPrice);
            })
        }
    </script>
</body>
</html>
